package servlet;

import entity.Owner;
import dao.OwnerDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private OwnerDao ownerDao = new OwnerDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        Owner owner = new Owner();
        owner.setUsername(request.getParameter("username"));
        owner.setPassword(request.getParameter("password"));
        owner.setName(request.getParameter("name"));
        owner.setBuilding(request.getParameter("building"));
        owner.setUnit(request.getParameter("unit"));
        owner.setRoom(request.getParameter("room"));
        owner.setType(request.getParameter("type"));
        owner.setPlateNumber(request.getParameter("plateNumber"));
        owner.setPhone(request.getParameter("phone"));

        try {
            ownerDao.register(owner);
            response.sendRedirect("login.jsp");
        } catch (Exception e) {
            request.setAttribute("error", "注册失败：" + e.getMessage());
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
    }
}